local n $n
local dataspec $dataspec
local prodspec $spec
local samplename $samplename

use "$working/$dataspec", clear

xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
gen diff_tfpr = tfpr - L.tfpr

foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate ln_`var' = ln(`var')
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_tfpr diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}

** To get the final sample for all regs:
	ivreghdfe drop_term ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2#year) first cluster(nic_08_4dig)
			generate sample0 = e(sample)
			keep if sample0 == 1

replace diff_tfpr = full_dif_TFPR // This is to make adjustments for changing production function parameters; this holds parameters fixed at lagged valued

preserve
	bysort co_code1 year: generate count_thing = _n
	keep if count_thing==1
	keep co_code1 year
	tempfile second_step
	save "$working//`samplename'_`prodspec'_sampleSS", replace
restore

foreach var in diff_tfpr dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {
preserve
* Baseline IV 		
	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year) first cluster(nic_08_4dig)
				local b`n'_y = _b[diff_ln_IND]
				local se_b`n'_y = _se[diff_ln_IND]
				cap local I_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_y = _se[diff_wIPT_IND_CHN]
				cap local I_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_y = e(N)
				mat f = e(first)
				local F_b`n'_y = round(f[8,1], .01)
				cap local F_I_b`n'_y = round(f[8,2], .01)
				

	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year nic2) first cluster(nic_08_4dig)
				local b`n'_yn = _b[diff_ln_IND]
				local se_b`n'_yn = _se[diff_ln_IND]
				cap local I_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_yn = e(N)
				mat f = e(first)
				local F_b`n'_yn = round(f[8,1], .01)
				cap local F_I_b`n'_yn = round(f[8,2], .01)

	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(nic2#year) first cluster(nic_08_4dig)
				local b`n'_ycn = _b[diff_ln_IND]
				local se_b`n'_ycn = _se[diff_ln_IND]
				cap local I_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_ycn = e(N)
				mat f = e(first)
				local F_b`n'_ycn = round(f[8,1], .01)
				cap local F_I_b`n'_ycn = round(f[8,2], .01)
		
* Baseline OLS
	* Year FE
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(year)  cluster(nic_08_4dig)
				local Ob`n'_y = _b[diff_ln_IND]
				local se_Ob`n'_y = _se[diff_ln_IND]
				cap local OI_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_OI_b2_y = _se[diff_wIPT_IND_CHN]
				cap local OI_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_OI_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local ON`n'_y = e(N)
			
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(year nic2)  cluster(nic_08_4dig)
				local Ob`n'_yn = _b[diff_ln_IND]
				local se_Ob`n'_yn = _se[diff_ln_IND]
				cap local OI_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_OI_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local OI_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_OI_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local ON`n'_yn = e(N)
		
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(nic2#year)  cluster(nic_08_4dig)
				local Ob`n'_ycn = _b[diff_ln_IND]
				local se_Ob`n'_ycn = _se[diff_ln_IND]
				cap local OI_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_OI_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local OI_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_OI_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local ON`n'_ycn = e(N)
			
* Reduced Form		
	reghdfe `var' $ivs1 $ivs2 $ctrl , a(year)  cluster(nic_08_4dig)
				local Rb`n'_y = _b[diff_ln_IVS]
				local se_Rb`n'_y = _se[diff_ln_IVS]
				cap local RI_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_RI_b2_y = _se[diff_wIPT_IND_CHN]
				cap local RI_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_y = e(N)
	
	reghdfe `var' $ivs1 $ivs2 $ctrl , a(year nic2)  cluster(nic_08_4dig)
				local Rb`n'_yn = _b[diff_ln_IVS]
				local se_Rb`n'_yn = _se[diff_ln_IVS]
				cap local RI_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_RI_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local RI_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_yn = e(N)

	reghdfe `var' $ivs1 $ivs2 $ctrl , a(nic2#year)  cluster(nic_08_4dig)
				local Rb`n'_ycn = _b[diff_ln_IVS]
				local se_Rb`n'_ycn = _se[diff_ln_IVS]
				cap local RI_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_RI_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local RI_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_ycn = e(N)

***
clear
set obs 1
gen bs = 0

gen `var'_b`n'_y = `b`n'_y'
cap gen `var'_I_b2_y = `I_b2_y'
cap gen `var'_I_b3_y = `I_b3_y'
gen `var'_N`n'_y = `N`n'_y'
gen `var'_F_b`n'_y = `F_b`n'_y'
cap gen `var'_F_I_b`n'_y = `F_I_b`n'_y'

gen `var'_b`n'_yn = `b`n'_yn'
cap gen `var'_I_b2_yn = `I_b2_yn'
cap gen `var'_I_b3_yn = `I_b3_yn'
gen `var'_N`n'_yn = `N`n'_yn'
gen `var'_F_b`n'_yn = `F_b`n'_yn'
cap gen `var'_F_I_b`n'_yn = `F_I_b`n'_yn'

gen `var'_b`n'_ycn = `b`n'_ycn'
cap gen `var'_I_b2_ycn = `I_b2_ycn'
cap gen `var'_I_b3_ycn = `I_b3_ycn'
gen `var'_N`n'_ycn = `N`n'_ycn'
gen `var'_F_b`n'_ycn = `F_b`n'_ycn'
cap gen `var'_F_I_b`n'_ycn = `F_I_b`n'_ycn'

gen `var'_Ob`n'_y = `Ob`n'_y'
cap gen `var'_OI_b2_y = `OI_b2_y'
cap gen `var'_OI_b3_y = `OI_b3_y'
gen `var'_ON`n'_y = `ON`n'_y'

gen `var'_Ob`n'_yn = `Ob`n'_yn'
cap gen `var'_OI_b2_yn = `OI_b2_yn'
cap gen `var'_OI_b3_yn = `OI_b3_yn'
gen `var'_ON`n'_yn = `ON`n'_yn'

gen `var'_Ob`n'_ycn = `Ob`n'_ycn'
cap gen `var'_OI_b2_ycn = `OI_b2_ycn'
cap gen `var'_OI_b3_ycn = `OI_b3_ycn'
gen `var'_ON`n'_ycn = `ON`n'_ycn'

gen `var'_Rb`n'_y = `Rb`n'_y'
cap gen `var'_RI_b2_y = `RI_b2_y'
cap gen `var'_RI_b3_y = `RI_b3_y'
gen `var'_RN`n'_y = `RN`n'_y'

gen `var'_Rb`n'_yn = `Rb`n'_yn'
cap gen `var'_RI_b2_yn = `RI_b2_yn'
cap gen `var'_RI_b3_yn = `RI_b3_yn'
gen `var'_RN`n'_yn = `RN`n'_yn'

gen `var'_Rb`n'_ycn = `Rb`n'_ycn'
cap gen `var'_RI_b2_ycn = `RI_b2_ycn'
cap gen `var'_RI_b3_ycn = `RI_b3_ycn'
gen `var'_RN`n'_ycn = `RN`n'_ycn'

save "$tempfold/bs`n'_`dataspec'_`var'", replace

*** SE
clear
set obs 1
gen bs = -1

gen `var'_b`n'_y = `se_b`n'_y'
cap gen `var'_I_b2_y = `se_I_b2_y'
cap gen `var'_I_b3_y = `se_I_b3_y'

gen `var'_b`n'_yn = `se_b`n'_yn'
cap gen `var'_I_b2_yn = `se_I_b2_yn'
cap gen `var'_I_b3_yn = `se_I_b3_yn'

gen `var'_b`n'_ycn = `se_b`n'_ycn'
cap gen `var'_I_b2_ycn = `se_I_b2_ycn'
cap gen `var'_I_b3_ycn = `se_I_b3_ycn'

gen `var'_Ob`n'_y = `se_Ob`n'_y'
cap gen `var'_OI_b2_y = `se_OI_b2_y'
cap gen `var'_OI_b3_y = `se_OI_b3_y'

gen `var'_Ob`n'_yn = `se_Ob`n'_yn'
cap gen `var'_OI_b2_yn = `se_OI_b2_yn'
cap gen `var'_OI_b3_yn = `se_OI_b3_yn'

gen `var'_Ob`n'_ycn = `se_Ob`n'_ycn'
cap gen `var'_OI_b2_ycn = `se_OI_b2_ycn'
cap gen `var'_OI_b3_ycn = `se_OI_b3_ycn'

gen `var'_Rb`n'_y = `se_Rb`n'_y'
cap gen `var'_RI_b2_y = `se_RI_b2_y'
cap gen `var'_RI_b3_y = `se_RI_b3_y'

gen `var'_Rb`n'_yn = `se_Rb`n'_yn'
cap gen `var'_RI_b2_yn = `se_RI_b2_yn'
cap gen `var'_RI_b3_yn = `se_RI_b3_yn'

gen `var'_Rb`n'_ycn = `se_Rb`n'_ycn'
cap gen `var'_RI_b2_ycn = `se_RI_b2_ycn'
cap gen `var'_RI_b3_ycn = `se_RI_b3_ycn'

save "$tempfold/bs`n'_`dataspec'_`var'_se", replace
restore
}

*** Compile results:
use "$tempfold/bs`n'_`dataspec'_diff_tfpr_se", clear
foreach var in dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {
	merge 1:1 bs using "$tempfold/bs`n'_`dataspec'_`var'_se", assert(3) nogen
	}
save "$tempfold/bs`n'_`dataspec'_se", replace

use "$tempfold/bs`n'_`dataspec'_diff_tfpr", clear
foreach var in dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {
	merge 1:1 bs using "$tempfold/bs`n'_`dataspec'_`var'", assert(3) nogen
	}
append using "$tempfold/bs`n'_`dataspec'_se"
order bs
save "$tempfold/bs`n'_`dataspec'", replace
